<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/27
 * Time: 22:07
 */
namespace app\model;


use think\facade\Db;
use think\Model;

class UserSign extends Model
{

    private static function _getListOutPutFields()
    {
        return [
            'us_id as signId',
            'us_uid as id',
            'us_pic as pic',
            'pername',
            'oreaddress',
            'us_start_time as signStartTime',
            'us_end_time as signEndTime',
        ];
    }
    public function addOneSign($data){
        $data['us_create_time'] = $data['us_create_time'] ?? time();
        return Db::table('user_sign')->insertGetId($data);
    }

    public function getSignInTotalNumByUid($uid){
        $where = [
            'us_uid' => $uid,
            'us_status' => 1
        ];
        return Db::table('user_sign')->where($where)->count();
    }

    public function getSignAllCountByWhere($where,$group = 'us_uid')
    {
        return Db::table('user_sign')->where($where)->group($group)->count();
    }

    public function getSignCountByWhere($where)
    {
        return Db::table('user_sign')->where($where)->count();
    }
    public function getSignInfoByWhere($where)
    {
        return Db::table('user_sign')->where($where)->find();
    }

    public function updateSignInfoById($signId,$data)
    {
        $data['us_update_time'] = time();
        return Db::table('user_sign')->where(['us_id'=>$signId])->save($data);
    }

    public function getSignListByWhere($where,$field = '')
    {
        $field = $field ? $field : self::_getListOutPutFields();
        return Db::table('user_sign')
            ->leftJoin('user','user.id=us_uid')
            ->leftJoin('organization','organization.id=us_orgid')
            ->where($where)
            ->field($field)
            ->order('us_id desc')
            ->select();
    }

    public function getHeadSignInfoByWhere($where,$field = '')
    {
        $field = $field ? $field : self::_getListOutPutFields();
        return Db::table('user_sign')
            ->leftJoin('user','user.id=us_uid')
            ->leftJoin('organization','organization.id=us_orgid')
            ->where($where)
            ->field($field)
            ->order('us_id desc')
            ->limit(1)
            ->find();
    }


    public function getSignPageListByWhere($where,$field = '')
    {
        $field = $field ? $field : self::_getListOutPutFields();
        return Db::table('user_sign')
            ->leftJoin('user','user.id=us_uid')
            ->leftJoin('organization','organization.id=us_orgid')
            ->where($where)
            ->field($field)
            ->order('us_id desc')
            ->paginate(input('param.pageSize'))
            ->render();
    }


    public function getOrgSignListByWhere($where)
    {
        $field = [
            'orgname',
            'count(us_id) as cnt'
        ];
        return Db::table('user_sign')
            ->leftJoin('user','user.id=us_uid')
            ->leftJoin('organization','organization.id=us_orgid')
            ->where($where)
            ->field($field)
            ->group('organization.id')
            ->select();
    }
}